package com.picture;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Random;

public class Picture extends JFrame {
    // 定义空白格的索引
    int x0;
    int y0;

    // 成功时的二维数组
    int[][] imageDataWin = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0},
    };

    // 定义二维数组，储存图片编号
    int[][] imageData = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0},
    };

    // 定义按钮
    JButton topButton;
    JButton footerButton;
    JButton leftButton;
    JButton rightButton;
    JButton helpButton;
    JButton resetButton;

    // 定义面板
    JPanel imagePanel;

    public Picture() {
        // 创建窗体
        initJFrame();

        // 打乱拼图
        upset();

        // 绘制窗体
        paintView();

        // 给按钮添加事件
        addButtonEvent();

        // 显示窗体
        this.setVisible(true);
    }

    public void upset() {
        Random r = new Random();
        for (int i = 0; i < imageData.length; i++) {
            for (int j = 0; j < imageData[i].length; j++) {
                int x = r.nextInt(imageData.length);
                int y = r.nextInt(imageData[i].length);
                int temp = imageData[x][y];
                imageData[x][y] = imageData[i][j];
                imageData[i][j] = temp;
            }
        }

        // 获取空白格的索引
        wc: for (int i = 0; i < imageData.length; i++) {
            for (int j = 0; j < imageData[i].length; j++) {
                if(imageData[i][j] == 0) {
                    x0 = i;
                    y0 = j;
                    break wc;
                }
            }
        }
    }

    // 添加按钮方法
    public void addButtonEvent() {
        topButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 边界处理
                if(x0 == 0) return;

                // 元素交换
                imageData[x0][y0] = imageData[x0 - 1][y0];
                imageData[x0 - 1][y0] = 0;
                x0 -= 1;

                // 判断移动是否成功
                if(isSuccess()) success();

                //重新绘制图形
                rePaintView();
            }
        });
        footerButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 边界处理
                if(x0 == 3) return;

                // 元素交换
                imageData[x0][y0] = imageData[x0 + 1][y0];
                imageData[x0 + 1][y0] = 0;
                x0 += 1;

                // 判断移动是否成功
                if(isSuccess()) success();

                //重新绘制图形
                rePaintView();
            }
        });
        leftButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 边界处理
                if(y0 == 0) return;

                // 元素交换
                imageData[x0][y0] = imageData[x0][y0 - 1];
                imageData[x0][y0 - 1] = 0;
                y0 -= 1;

                // 判断移动是否成功
                if(isSuccess()) success();

                //重新绘制图形
                rePaintView();
            }
        });
        rightButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 边界处理
                if(y0 == 3) return;

                // 元素交换
                imageData[x0][y0] = imageData[x0][y0 + 1];
                imageData[x0][y0 + 1] = 0;
                y0 += 1;

                // 判断移动是否成功
                if(isSuccess()) success();

                //重新绘制图形
                rePaintView();
            }
        });
        helpButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                success();
                rePaintView();
            }
        });
        resetButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                imageData = new int[][] {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                        {13, 14, 15, 0},
                };
                upset();
                rePaintView();
                topButton.setEnabled(true);
                footerButton.setEnabled(true);
                leftButton.setEnabled(true);
                rightButton.setEnabled(true);
            }
        });
    }

    public void rePaintView() {
        imagePanel.removeAll();

        imagePanel = new JPanel();
        imagePanel.setBounds(150, 114, 360, 360);
        imagePanel.setLayout(null);
        for (int i = 0; i < imageData.length; i++) {
            for (int j = 0; j < imageData[i].length; j++) {
                JLabel imageLabel = new JLabel(new ImageIcon("images/" + imageData[i][j] + ".png"));
                imageLabel.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imageLabel);
            }
        }
        this.add(imagePanel);

        // 重新绘制
        imagePanel.repaint();
    }
    public void success() {
        // 完整拼图二维数组
        imageData = new int[][] {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16},
        };
        topButton.setEnabled(false);
        footerButton.setEnabled(false);
        leftButton.setEnabled(false);
        rightButton.setEnabled(false);
    }

    public boolean isSuccess() {
        for (int i = 0; i < imageData.length; i++) {
            for (int j = 0; j < imageData[i].length; j++) {
                if(imageData[i][j] != imageDataWin[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    private void paintView() {
        // 窗体标题
        JLabel titleLabel = new JLabel(new ImageIcon("images/title.png"));
        titleLabel.setBounds(354, 27, 232, 57);
        this.add(titleLabel);

        // 画版图
        imagePanel = new JPanel();
        imagePanel.setBounds(150, 114, 360, 360);
        imagePanel.setLayout(null);

        // 4 * 4t 拼图
        for (int i = 0; i < imageData.length; i++) {
            for (int j = 0; j < imageData[i].length; j++) {
                JLabel imageLabel = new JLabel(new ImageIcon("images/" + imageData[i][j] + ".png"));
                imageLabel.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imageLabel);
            }
        }
        this.add(imagePanel);

        // 参考图
        JLabel consultImg = new JLabel();
        consultImg.setBounds(574, 114, 122, 121);
        this.add(consultImg);

        // 按钮（上下左右）
        topButton = new JButton(new ImageIcon("images/shang.png"));
        topButton.setBounds(732, 265, 57, 57);
        this.add(topButton);

        footerButton = new JButton(new ImageIcon("images/xia.png"));
        footerButton.setBounds(732, 347, 57, 57);
        this.add(footerButton);

        leftButton = new JButton(new ImageIcon("images/zuo.png"));
        leftButton.setBounds(650, 347, 57, 57);
        this.add(leftButton);

        rightButton = new JButton(new ImageIcon("images/you.png"));
        rightButton.setBounds(813, 347, 57, 57);
        this.add(rightButton);

        // 按钮（帮助、重置）
        helpButton = new JButton(new ImageIcon("images/qiuzhu.png"));
        helpButton.setBounds(626, 444, 108, 45);
        this.add(helpButton);

        resetButton = new JButton(new ImageIcon("images/chongzhi.png"));
        resetButton.setBounds(786, 444, 108, 45);
        this.add(resetButton);

        // 窗体背景
        JLabel backGroundLabel = new JLabel(new ImageIcon("images/background.png"));
        backGroundLabel.setBounds(0, 0, 960, 530);
        this.add(backGroundLabel);
    }

    private void initJFrame() {
        this.setTitle("动漫拼图");
        this.setSize(960, 530);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);
        this.setAlwaysOnTop(true);
    }
}
